package com.btr.ivle.backgroud;

import java.net.URL;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.os.AsyncTask;

import com.btr.ivle.R;
import com.btr.ivle.data.IVLEToken;
import com.btr.ivle.helper.DatabaseEvent;
import com.btr.ivle.helper.URLtoString;

public class GetEventsTask extends AsyncTask<Void, Void, Void>{
	
	private Context context;
	
	public GetEventsTask(Context context){
		this.context = context;
	}

	protected Void doInBackground(Void... voids) {
		String ID, Title, Description, EventDateTime, Organizer, Venue, Price, Agenda, Contact;
		
		try{
        	URL url = new URL("https://ivle.nus.edu.sg/api/Lapi.svc/StudentEvents?APIKey=" +
        			context.getString(R.string.IVLE_API_KEY) + "&AuthToken=" + IVLEToken.getToken() + "&TitleOnly=false");
        	
        	JSONObject jObject = new JSONObject(URLtoString.get(url));
	        JSONArray resultsArrary = jObject.getJSONArray("Results");
	        
	        DatabaseEvent dbEvent = new DatabaseEvent(this.context,IVLEToken.getUserID()+"_Event",null,context.getResources().getInteger(R.integer.DBVersion));
	        String table = IVLEToken.getUserID() + "_Event";
	        ContentValues values;

	        String selection = dbEvent.isEvent + "=?";
			String[] selectionArgs = new  String[] {"1"};
	        dbEvent.getWritableDatabase().delete(table, selection, selectionArgs);
	        
	        for (int i = 0; i < resultsArrary.length(); i++) {
	        	ID = resultsArrary.getJSONObject(i).getString("ID").toString();
	        	Title = resultsArrary.getJSONObject(i).getString("Title").toString();
	        	Description = resultsArrary.getJSONObject(i).getString("Description").toString();
	        	EventDateTime = resultsArrary.getJSONObject(i).getString("EventDateTime").toString();
	        	Organizer = resultsArrary.getJSONObject(i).getString("Organizer").toString();
	        	Venue = resultsArrary.getJSONObject(i).getString("Venue").toString();
	        	Price = resultsArrary.getJSONObject(i).getString("Price").toString();
	        	Agenda = resultsArrary.getJSONObject(i).getString("Agenda").toString();
	        	Contact = resultsArrary.getJSONObject(i).getString("Contact").toString();

        		values = new  ContentValues();
	        	values.put(dbEvent.ID, ID);
	        	values.put(dbEvent.Title, Title);
	        	values.put(dbEvent.Description, Description);
	        	values.put(dbEvent.EventDateTime, EventDateTime);
	        	values.put(dbEvent.Organizer, Organizer);
	        	values.put(dbEvent.Venue, Venue);
	        	values.put(dbEvent.Price, Price);
	        	values.put(dbEvent.Agenda, Agenda);
	        	values.put(dbEvent.Contact, Contact);
	        	values.put(dbEvent.isEvent, true);
	        	dbEvent.getReadableDatabase().replace(table, null, values);
	        	
			}
	        dbEvent.getReadableDatabase().close();
	        dbEvent.close();
	        
        }catch(Exception ex){
	    }
		return null;
	}
	
    @SuppressWarnings("unchecked")
	protected void onPostExecute(Void result) {
    	AsyncTaskCompleteListener<String> l = (AsyncTaskCompleteListener<String>) context;
    	l.onAsyncTaskComplete("");
    }
}
